package com.hdw.auth.controller;

import com.hdw.auth.api.SysFucaService;
import com.hdw.auth.api.SysUseTagService;
import com.hdw.auth.bean.model.SysFuca;
import com.hdw.auth.bean.model.SysUseTag;
import com.hdw.auth.utils.AuthUtil;
import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.util.TreeUtil;
import com.hdw.common.vo.LayTreeVO;
import com.hdw.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sysUseTag控制器
 * Created by hozeData on 2021/11/21.
 */
@RestController
@RequestMapping("/auth/sysUseTag/")
@Api( tags="用户标签管理")
public class SysUseTagController {

    @Autowired
    SysUseTagService sysUseTagService;
    @Autowired
    SysFucaService sysFucaService;

    @ApiOperation(value="按用户查询")
    @RequestMapping(value="findByUseId", method=RequestMethod.GET)
    public BaseResult findByUseId(int useId) {
        List<Map<String,Object>> list = sysUseTagService.findByUseId(useId);
        List<SysFuca> fucaList = sysFucaService.findByModel(new SysFuca());
        Map<String, LayTreeVO> treeMap = new HashMap<>();
        LayTreeVO tree;
        AuthUtil.fucaToMaptree(treeMap,fucaList);
        for(Map<String,Object> map:list) {
            tree = new LayTreeVO();
            tree.setTitle(map.get("description").toString());
            tree.setParent(map.get("id_fuca")==null?"":map.get("id_fuca").toString());
            tree.setData(map);
            tree.setId("tag_"+map.get("id"));
            if(map.get("use_id")!=null) {
                tree.setChecked(true);
            }
            treeMap.put("tag_"+map.get("id"),tree);
        }
        List<LayTreeVO> treeList = TreeUtil.treeHandler(treeMap);
        return new BaseResult(CommonResultConstant.SUCCESS, treeList);
    }

    @ApiOperation(value="新增")
    @ApiImplicitParam(name="sysUseTag", value="实体", paramType="body", required=true, dataType="SysUseTag")
    @RequestMapping(value="insert", method=RequestMethod.POST)
    public BaseResult insert(@RequestBody SysUseTag sysUseTag, HttpServletRequest request)  {
        int num = sysUseTagService.insertSelective(sysUseTag);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="id", value="主键", paramType="query", required=true, dataType="Integer")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Integer id, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("请选择要编辑的项");
        }
        SysUseTag sysUseTag = sysUseTagService.getById(id);
        return new BaseResult(CommonResultConstant.SUCCESS, sysUseTag);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="sysUseTag", value="实体", paramType="body", required=true, dataType="SysUseTag")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody SysUseTag sysUseTag, HttpServletRequest request)  {
        int num = sysUseTagService.updateByIdSelective(sysUseTag);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Integer tagId,Integer useId) throws BaseException {
        int num = sysUseTagService.delete(useId,tagId);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
